/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web.firm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.modules.sys.entity.customer.Customer;
import com.thinkgem.jeesite.modules.sys.entity.defaulttime.DefaultTime;
import com.thinkgem.jeesite.modules.sys.entity.firm.tonji;
import com.thinkgem.jeesite.modules.sys.service.DictService;
import com.thinkgem.jeesite.modules.sys.service.defaulttime.DefaultTimeService;
import org.activiti.engine.impl.util.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.firm.Firm;
import com.thinkgem.jeesite.modules.sys.service.firm.FirmService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * firmController
 * @author jin
 * @version 2018-04-23
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/firm/firm")
public class FirmController extends BaseController {

	@Autowired
	private FirmService firmService;
	@Autowired
    private DefaultTimeService defaultTimeService;
	@Autowired
    private DictService dictService;
	
	@ModelAttribute
	public Firm get(@RequestParam(required=false) String id) {
		Firm entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = firmService.get(id);
		}
		if (entity == null){
			entity = new Firm();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:firm:firm:view")
	@RequestMapping(value = {"list", ""})
	public String list(Firm firm, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Firm> page = firmService.findPage(new Page<Firm>(request, response), firm); 
		model.addAttribute("page", page);
		return "modules/sys/firm/firmList";
	}

	@RequiresPermissions("sys:firm:firm:view")
	@RequestMapping(value = "cqtx")
	public String cqtx(Firm firm, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Firm> page = firmService.findPage1(new Page<Firm>(request, response), firm);
		model.addAttribute("page", page);
		return "modules/sys/firm/firmList1";
	}

	@RequiresPermissions("sys:firm:firm:view")
	@RequestMapping(value = "form")
	public String form(Firm firm, Model model) {
		model.addAttribute("firm", firm);
		return "modules/sys/firm/firmForm";
	}

	@RequiresPermissions("sys:firm:firm:edit")
	@RequestMapping(value = "save")
	public String save(Firm firm, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, firm)){
			return form(firm, model);
		}
		firmService.save(firm);
		addMessage(redirectAttributes, "保存业务成功");
		return "redirect:"+Global.getAdminPath()+"/sys/firm/firm/?repage";
	}
	
	@RequiresPermissions("sys:firm:firm:del")
	@RequestMapping(value = "delete")
	public String delete(Firm firm, RedirectAttributes redirectAttributes) {
		firmService.delete(firm);
		addMessage(redirectAttributes, "删除业务成功");
		return "redirect:"+Global.getAdminPath()+"/sys/firm/firm/?repage";
	}

	@RequestMapping(value = "huifang")
	public String huifang(String id,Model model){
		Firm firm = firmService.get(id);
		model.addAttribute("firm",firm);
		return "modules/sys/firm/huiFang";
	}

    @ResponseBody
    @RequestMapping(value = "saveHuiFang")
    public Object saveHuiFang(Firm firm) throws ParseException {
	    if(firm!= null){
	        firm.setYwStatic("3");
	        firm.setHfStime(new Date());
	        if(firm.getChuTime()!=null){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date before = sdf.parse(sdf.format(new Date()));
				Date now=sdf.parse(sdf.format(firm.getChuTime()));
				long l=before.getTime()-now.getTime();
				long day=l/(24*60*60*1000);
				firm.setCxSx(String.valueOf(day));
			}
	        firmService.save(firm);
	        return true;
        }else {
	        return false;
        }
    }

	@ResponseBody
	@RequestMapping(value = "saveHuiXin")
	public Object saveHuiXin(Firm firm) {
		if(firm!= null){
			firmService.save(firm);
			return true;
		}else {
			return false;
		}
	}

    @RequestMapping(value = "shouli")
    public String shouli(String id,Model model){
        Firm firm = firmService.get(id);
        model.addAttribute("firmlist",firm);
        return "modules/sys/firm/shouLi";
    }

    @ResponseBody
    @RequestMapping(value = "saveShouli")
    public Object saveShouli (Firm firm) {
        if(firm!= null){
        	int num = 0; Boolean  ss=false;
            firm.setYwStatic("4");
            firm.setSlTime(new Date());
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            DefaultTime defaultTime = defaultTimeService.get("1");
            if(firm.getDifficulty().equals("1")) {
				 num = Integer.valueOf(defaultTime.getSlJday());
			}
			if(firm.getDifficulty().equals("2")) {
				 num = Integer.valueOf(defaultTime.getSlDay());
			}
			if(num!=0) {
				c.add(Calendar.DAY_OF_MONTH, num);
				firm.setSlFtime(c.getTime());
				firm.setShou(String.valueOf(num));
				firm.setSlSx("0");
				firmService.save(firm);
				ss=true;
			}
			return ss;
        }else {
            return false;
        }
    }


    @RequestMapping(value = "jiean")
    public String jiean(String id,Model model){
        Firm firm = firmService.get(id);
        model.addAttribute("firm",firm);
        return "modules/sys/firm/jieAn";
    }

    @ResponseBody
    @RequestMapping(value = "saveJieAn")
    public Object saveJieAn(Firm firm){
        if(firm.getId() !=null || firm.getId().length()>0){
            firm.setYwStatic("6");
            firm.setSlStime(new Date());
            firmService.save(firm);
            return true;
        }else {
            return false;
        }
    }

	@ResponseBody
	@RequestMapping(value = "saveChe")
	public Object saveChe(Firm firm){
		if(firm.getId() !=null || firm.getId().length()>0){
			firm.setYwStatic("7");
			firm.setCjTime(new Date());
			firmService.save(firm);
			return true;
		}else {
			return false;
		}
	}

	@RequestMapping(value = "xinxi")
	public String xinxi(String id,Model model){
		Firm firm = firmService.get(id);
		model.addAttribute("firm",firm);
		return "modules/sys/firm/xinXi";
	}

	@RequestMapping(value = "remark")
	public String remark(String id,Model model){
		Firm firm = firmService.get(id);
		model.addAttribute("firm",firm);
		return "modules/sys/firm/Remark";
	}

    @RequestMapping(value = "tbxx")
    public String tbxx(String id,Model model){
        Firm firm = firmService.get(id);
        model.addAttribute("firm",firm);
        return "modules/sys/firm/tbxx";
    }
    @RequestMapping(value = "chuxx")
    public String chuxx(String id,Model model){
        Firm firm = firmService.get(id);
        model.addAttribute("firm",firm);
        return "modules/sys/firm/chuxx";
    }
    @RequestMapping(value = "sbzd")
    public String sbzd(String id,Model model){
        Firm firm = firmService.get(id);
        model.addAttribute("firm",firm);
        return "modules/sys/firm/sbzd";
    }

	@ResponseBody
	@RequestMapping(value = "saveRemark")
	public Object saveRemark(Firm firm){
		if(firm.getId() !=null || firm.getId().length()>0){
			firmService.save(firm);
			return true;
		}else {
			return false;
		}
	}

	@ResponseBody
	@RequestMapping(value = "savechuXinxi")
	public Object savechuXinxi(Firm firm){
		if(firm.getId() !=null || firm.getId().length()>0){
			firmService.save(firm);
			return true;
		}else {
			return false;
		}
	}

	@ResponseBody
	@RequestMapping(value = "savetbXinxi")
	public Object savetbXinxi(Firm firm){
		if(firm.getId() !=null || firm.getId().length()>0){
			firmService.save(firm);
			return true;
		}else {
			return false;
		}
	}

	@ResponseBody
	@RequestMapping(value = "savesbZd")
	public Object savesbZd(Firm firm){
		if(firm.getId() !=null || firm.getId().length()>0){
			firmService.save(firm);
			return true;
		}else {
			return false;
		}
	}

	@RequestMapping(value = "tonJi")
	public String tonJi(String officeId,Model model){
		List<Map<String,Object>> list = firmService.tonJi(officeId);
		String ss = firmService.tonJijg(officeId);
        model.addAttribute("ss",ss);
        model.addAttribute("list",list);
        model.addAttribute("officeId",officeId);
		return "modules/sys/firm/tonJi";
	}

	@ResponseBody
	@RequestMapping(value = "jgtonji")
	public Object jgtonji(String officeId){
		List<Map<String,Object>> list = firmService.tonJi(officeId);
		Map<String,Object> ma1 = new HashMap<String,Object>();
		ma1.put("1","0");ma1.put("2","0");ma1.put("3","0");ma1.put("4","0");ma1.put("5","0");ma1.put("6","0");ma1.put("7","0");
		if(list.size()>0 || list != null){
			for (Map<String,Object> item:list){
				ma1.put(item.get("zt").toString(), item.get("num").toString());
			}
		}
		return ma1;
	}

	@RequiresPermissions("sys:firm:firm:view")
	@RequestMapping(value = "xiugai")
	public String xiugai(Firm firm, Model model) {
		model.addAttribute("firm", firm);
		return "modules/sys/firm/firmxiugai";
	}


	@RequestMapping(value = "bingTu")
	public String binfTu(){
		return "modules/sys/firm/bingMap";
	}

	@ResponseBody
	@RequestMapping(value = "bingMap")
	public Object bingMap(){
		List<Map<String,Object>> list = firmService.bingMap();
		Map<String,Object> ma1 = new HashMap<String,Object>();
		ma1.put("1","0");ma1.put("2","0");ma1.put("3","0");ma1.put("4","0");ma1.put("5","0");ma1.put("6","0");ma1.put("7","0");
		if(list.size()>0 || list != null){
			for (Map<String,Object> item:list){
				ma1.put(item.get("zt").toString(), item.get("num").toString());
			}
		}
		return ma1;
	}

	@ResponseBody
    @RequestMapping(value = "getYwstatic")
    public Object getYwstatic(String id){
	    int i = Integer.valueOf(id)+1;
	    List<Map<String,Object>> list = firmService.getBing(String.valueOf(i));
        Map<String, Object> testMap = new HashMap<String, Object>();
        testMap.put("num",dictService.getYwstatName(String.valueOf(i)));
        testMap.put("name",String.valueOf(i));
	    list.add(testMap);
        Map<String,Object> ma1 = new HashMap<String,Object>();
        if(list.size()>0 || list != null){
            for (Map<String,Object> item:list){
                ma1.put(item.get("name").toString(), item.get("num").toString());
            }
        }
	    return list;
    }



	@RequestMapping(value = "ztonji")
	public String ztonji( Model model) {
		List<tonji> tj = firmService.ztonji();
		List<String> list1 = new ArrayList();List list2 = new ArrayList();List list3 = new ArrayList();List list4 = new ArrayList();List list5 = new ArrayList();List list6 = new ArrayList();List lista = new ArrayList();List list7 = new ArrayList();List list8 = new ArrayList();
		for( tonji ts:tj){
			list1.add(ts.getSt1());lista.add(ts.getName());
			list2.add(ts.getSt2());list3.add(ts.getSt3());list4.add(ts.getSt4());list5.add(ts.getSt5());list6.add(ts.getSt6());list7.add(ts.getSt7());list8.add(ts.getSt8());
		}
		model.addAttribute("list1", list1);model.addAttribute("list2", list2);model.addAttribute("list3", list3);model.addAttribute("list4", list4);
		model.addAttribute("list5", list5);model.addAttribute("list6", list6);model.addAttribute("list7", list7);model.addAttribute("lista",lista);model.addAttribute("list7", list7);model.addAttribute("list8", list8);
		model.addAttribute("tj", tj);
		return "modules/sys/firm/ztonjiMap";
	}

	/**
	 * 导出用户数据
	 * @param firm
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
//	@RequiresPermissions("sys:customer:customer:view")
	@RequestMapping(value = "export", method= RequestMethod.POST)
	public String exportFile(Firm firm, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "用户数据"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
//			Page<Firm> page = firmService.findUser(new Page<Firm>(request, response, -1), firm);
			List<Firm> list = firmService.getchoose(firm);
			new ExportExcel("用户数据", Firm.class).setDataList(list).write(response, fileName).dispose();
//			new ExportExcel("用户数据", Firm.class).setDataList(page.getList()).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出用户失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/sys/firm/firm/?repage";
	}

	@RequestMapping(value = "cqtpexport", method= RequestMethod.POST)
	public String cqtpexportFile(Firm firm, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "用户数据"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			List<Firm> list = firmService.getchoose1(firm);
			new ExportExcel("用户数据", Firm.class).setDataList(list).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出用户失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/sys/firm/firm/cqtp?repage";
	}

}